﻿namespace DCHealthCheck.MvcWeb.Controllers {

    #region Usings
    using DCHealthCheck.DomainModel;
    using DCHealthCheck.MvcWeb.Classes;
    using DCHealthCheck.MvcWeb.Classes.DataServices;
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc; 
    #endregion

    public class DCController : BaseController {

        private readonly IDCDataService DCDataService;
        private readonly IRepository<DomainController, IEntityManager> DCRepository;

        public DCController(IDCDataService dcDataService, IRepository<DomainController, IEntityManager> dcRepository) {
            this.ControllerName = "DC";
            this.DCDataService = dcDataService;
            this.DCRepository = dcRepository;
        }

        [Authorize(Roles = Global.AdminRole)]
        [HttpPost]
        public ActionResult DeleteDC(Guid id) {
            bool success = false;

            var domainController = this.DCRepository.Get(x => x.Id == id).FirstOrDefault();
            if (domainController != null) {
                domainController.DetachedState = DetachedState.Deleted;
                this.DCRepository.Delete(domainController);
                this.DCRepository.Save();

                success = this.DCRepository.Get(x => x.Id == id).FirstOrDefault() == null;
            }

            return success ? new HttpStatusCodeResult(200, "Ok") : new HttpStatusCodeResult(500, "Error occurred performing the requested action.");
        }

        public ActionResult Index(DCViewModel viewModel) {
            this.DCDataService.ProcessData(viewModel);
            return View(viewModel);
        }

    }
}
